package com.lztx.shared.shared.util.andpermission;

import android.app.Activity;
import android.support.annotation.NonNull;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;

/**
 * @name joy
 * @class name：com.lztx.joy.shared.utils
 * @describe 权限请求Utils  基于AndPermission的二次封装
 * @anthor XINDZ
 * @time 2017/11/14 11:48
 * @change
 * @chang time
 * @class describe
 */
public class PermissionsUtils {

    public interface PerListener {
        void hasPermissions();

        void noPermissions();
    }

    /**
     * 请求权限
     *
     * @param listener
     * @param activity
     * @param requestCode
     * @param permissionsArray
     */
    public static void requestPermission(final Activity activity,final int requestCode,final PerListener listener,final String[]... permissionsArray) {
        PermissionListener permissionListener = new PermissionListener() {

            @Override
            public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                onResult(requestCode, activity, listener, permissionsArray);

            }

            @Override
            public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                // 是否有不再提示并拒绝的权限。
                onResult(requestCode, activity, listener, permissionsArray);

            }
        };
        AndPermission
                .with(activity)
                .requestCode(requestCode)
                .permission(permissionsArray)
                .rationale(new RationaleListener() {
                               @Override
                               public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                                   AndPermission.rationaleDialog(activity, rationale).show();
                               }
                           }
                )
                .callback(permissionListener)
                .start();
    }

    private static void onResult(int requestCode, Activity activity, PerListener listener, String[]... permissionsArray) {
        boolean isResult = true;
        for (String[] s : permissionsArray) {
            boolean p = AndPermission.hasPermission(activity, s);
            isResult = isResult && p;
        }
        // 是否有不再提示并拒绝的权限。
        if (isResult) {
            // TODO 执行拥有权限时的下一步。
            listener.hasPermissions();
        } else {
            // 使用AndPermission提供的默认设置dialog，用户点击确定后会打开App的设置页面让用户授权。
            AndPermission.defaultSettingDialog(activity, requestCode).show();
            listener.noPermissions();
        }
    }

    /**
     * 检查是否有权限
     *
     * @param activity
     * @param permissionsArray
     * @return
     */
    public static boolean checkPermissions(Activity activity, String[]... permissionsArray) {
        boolean isResult = true;
        for (String[] s : permissionsArray) {
            boolean p = AndPermission.hasPermission(activity, s);
            isResult = isResult && p;
        }
        return isResult;
    }
}
